package homework.day15;

import java.util.ArrayList;
import java.util.List;

public class Test02 {

    public static void main(String[] args) {
        Set set = new Set();
        Thread t1 = new AddThread(set);
        Thread t2 = new GetThread(set);
        t1.start();
        t2.start();
    }
}

class Set{
    private List<Integer> list = new ArrayList<>();

    public synchronized int getList(int i) {
        if (list.size()<40){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return list.get(i);
    }

    public synchronized void setList(int i) {

        if (list.size()>=40){
            notifyAll();

        }
        list.add(i);
    }
}

class AddThread extends Thread{
    private Set set;

    public AddThread(Set set){
        this.set = set;
    }

    @Override
    public void run() {
        for (int i =-100;i<=100;i++){
            System.out.println(Thread.currentThread().getName()+"放入"+ i);
            set.setList(i);


        }
    }
}

class GetThread extends Thread{
    private Set set;
    private int i;

    public GetThread(Set set){
        this.set = set;

    }

    @Override
    public void run() {
        while (true) {
            System.out.println(set.getList(i));
            ++i;

        }

    }

    }
